import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { ElNotification } from "element-plus"; //处理导航信息
import { WarningItem } from "@/api/backend/index";
import { mapCompanies } from "@/utils/common";

export const useWarningItem = defineStore(Names.WarningItem, {
  state: () => {
    return {
      ruleId: <any>null,
      ruleName: "",
      single: <any>{},
      tags: <any>{},
      type: <any>[],
      typeSymbol: <any>[], //判定符号
      typeGrainSize: <any>[], //统计粒度
    };
  },
  getters: {},
  actions: {
    //获取告警指标列表数据
    getWarningItemList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(WarningItem + "/list", filter, "json", "").then((res: any) => {
          console.log("获取告警指标列表数据", res);
          resolve(res);
        });
      });
    },
    //删除指标项
    delete(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(WarningItem + `/${data.itemId}`, {}, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    add(data: any) {
      return new Promise((resolve, reject) => {
        _post(WarningItem, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    edit(data: any) {
      return new Promise((resolve, reject) => {
        _put(WarningItem, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
